// ==UserScript==
// @name         *booru arrow navigation
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Press button - move page
// @author       author.name=qaplwsokedij
// @match        https://safebooru.org/index.php?page=post*
// @match        https://gelbooru.com/index.php*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    const rightArrow = 'ArrowRight';
    const leftArrow = 'ArrowLeft';
    if (document.location.host == "safebooru.org") {
        window.addEventListener('keydown', function(event) {
            switch (event.key) {
                case rightArrow:
                    document.querySelector('.pagination > a:nth-child(14)').click();
                    break;
                case leftArrow:
                    document.querySelector('.pagination > a:nth-child(2)').click();
                    break;
            }
        })
    }
    if (document.location.host == "gelbooru.com") {
        window.addEventListener('keydown', function(event) {
            // Page selectors position
            const pos = Array.from(document.querySelector('#paginator').children).indexOf(document.querySelector('#paginator').querySelector('b'));
            const prevPos = document.querySelector('#paginator').children[pos - 1];
            const nextPos = document.querySelector('#paginator').children[pos + 1];

            switch (event.key) {
                case rightArrow:
                    if (pos < document.querySelector('#paginator').children.length - 1) {
                        nextPos.click();
                    }
                    break;
                case leftArrow:
                    if (pos >= 1) {
                        prevPos.click();
                    }
                    break;
            }
        })
    }
})();
